package org.example.student_system.interceptor;

import io.jsonwebtoken.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
public class Interceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        log.info("获取token: {}", token);
        if (token != null&& !token.equals("undefined")) {
            try {
                Claims claims = Jwts.parser().setSigningKey("liangweijun").parseClaimsJws(token).getBody();
                log.info("token解析成功: {}", claims);
                return true;
            } catch (Exception e) {
                log.error("token错误: {}", e.getMessage());
                return false;
            }
        }
        log.error("没有token");
        return false;
    }
}
